package org.ycx.upms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.ycx.upms.common.constants.SecurityConstants;
import org.ycx.upms.service.IIndexService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by 杨 on 2017-06-28.
 */
@Controller
public class IndexController {

    @Value("${app.id}")
    private Integer appId;

    @Autowired
    private IIndexService service;

    @RequestMapping("index.html")
    public String indexPage(HttpServletRequest request, HttpSession session) {
        if (!"1".equals(session.getAttribute(SecurityConstants.ISLOGIN))) {
            SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
            String loginName = securityContextImpl.getAuthentication().getName();
            Map<String,Object> sessionInfo = this.service.findUserInfoAndSysInfo(loginName,appId);
            for (Map.Entry<String,Object> ent:sessionInfo.entrySet ()){
                session.setAttribute(ent.getKey (), ent.getValue ());
            }
            session.setAttribute (SecurityConstants.ISLOGIN,true);
        }
        return "index";
    }

    @RequestMapping("/")
    public String welComePage(){
        return "redirect:/index.html";
    }

    @RequestMapping("login.html")
    public String loginPage() {
        return "login";
    }
}
